<template>
  <div class="tutionProcess">
    <div>str: {{str}}</div>
    <div>encryptedData: {{encryptedData}}</div>
    <div>iv: {{iv}}</div>
    <button open-type="share">转发</button>
    <button v-if="!accredit" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getuserinfo">确定</button>
  </div>
</template>

<script>
import qcloud from "../../../node_modules/wafer2-client-sdk/index.js";
import { obtainShareTopSpot,obtainGroup,obtainBatchTopSpot} from "@/config/api";
import { obtainAccreditLoginFlag ,showMessager ,obtainWxLoginCode,obtainShareInfo} from "@/config/wUtils";
import { baseUrl } from "@/config/env";
export default {
    data () {
        return {
            str : '',
            encryptedData: '',
            iv: '',
            accredit: true,
            // 分享的批次ID
            shareBatchId: '',
            // 分享群的凭证   
            shareTicket: '',
            // 群聊ID
            shareGroupId: '',
            // 批次ID
            batch_id: '',
            // 微信场景
            wx_scene: '',
            // 页面访问历史
            historyPage: [],
            // 当前界面
            currentPage: 1
        }
    },
    onShow() { 
        this.historyPage = getCurrentPages()
        this.wx_scene = wx.getStorageSync("wx_scene");
        if (this.wx_scene != 1044 && this.historyPage.length === 1) {
            this.str = '';
            this.iv = '';
        } else {
            this.init()
        }
    },
    async mounted() {
        
    },
    methods: {
        async init () {
            this.currentPage = 1;
            this.batch_id = wx.getStorageSync("batch_id");
            // scene 1044是群聊分享 1007是单聊分享
            if (this.historyPage.length === 1 && this.wx_scene === 1044) { 
                // this.historyPage长度为1，证明是转发页面
                // this.getUserData()
                this.shareBatchId = wx.getStorageSync("share_group_batch_id");
                this.shareTicket = wx.getStorageSync("shareTicket");
                // this.shareTicket 存在的话，证明是转发到群聊
                if (this.shareBatchId && this.shareTicket) {
                    await this.getObtaintopSpot(this.shareBatchId,this.currentPage)
                }
            } else if (this.historyPage.length > 1 && this.batch_id ) {
                // pages长度大于1，批次存在说明
                await this.getObtainBatchTopSpot(this.currentPage)
            }
        },
        /* 获取用户信息 */
        async getObtaintopSpot (batch_id,page) {
            let self = this;
            // 获取code
            await obtainWxLoginCode()
                .then( async res => {
                    if (res.code) {
                        console.log('用户',res)
                        console.log('用户',batch_id)
                        // 必须是在用户已经授权的情况下调用     // 获取群id
                        await obtainShareInfo(self.shareTicket)
                            .then( async res1 => {
                                console.log('分享成功',res1)
                                if ( batch_id && batch_id !='undefined') {
                                    // 获取学员学习和群ID，并且校验学员是否报名此课程
                                    await obtainGroup(res1.encryptedData,res1.iv,res.code,batch_id)
                                        .then(async data => {
                                            console.log('成员',data.status)
                                            if (data.status === 200) {
                                                wx.setStorageSync("share_group_user_info", data.data.userInfo);
                                                self.shareGroupId = data.data.group_id;
                                                // 获取群排行榜
                                                await self.getObtainTopSpot(batch_id,page,self.shareGroupId)
                                            } else if (data.status === 401) {
                                                // 用户未授权
                                                self.accredit = false;
                                            } else {        
                                                // 错误信息
                                                await showMessager(data.msg)
                                            }
                                        })
                                        .catch( async err => {
                                             await showMessager('网络拥堵，请稍后再试')
                                        })
                                }
                            })
                    }
                })
        },
        // 获取群聊排行榜
        async getObtainTopSpot (batch_id,page,group_id) {
            let self = this;
            await obtainShareTopSpot(batch_id,page,group_id)  
                .then(res => {
                    console.log('群聊排行榜信息',res)
                    self.iv = res.data.list.length || 0
                })
        },
        // 获取批次下的排行榜
        async getObtainBatchTopSpot (page) {
            await obtainBatchTopSpot(this.batch_id,page)
                .then( res => {
                    console.log('批次排行榜信息',res)
                })
        },
        // 登录
        async getuserinfo() {
            const session = await qcloud.Session.get();
            if (session) {
                // 第二次登录
                // 或者本地已经有登录态
                // 可使用本函数更新登录态
                await qcloud.setLoginUrl(baseUrl + "/weapp/login");
                await qcloud.loginWithCode({
                    success: res => {
                        this.userInfo = res;
                        wx.setStorageSync("userInfo", res);
                        // 成功授权，并且重新请求排行榜
                        self.accredit = true;
                        this.getObtaintopSpot(this.shareBatchId,1)
                    },
                    fail: err => {
                        console.error(err);
                        showMessager("登录错误");
                    }
                });
            } else {
                // 首次登录
                await qcloud.setLoginUrl(baseUrl + "/weapp/login");
                await qcloud.login({
                    success: res => {
                        this.userInfo = res;
                        wx.setStorageSync("userInfo", res); 

                        // 成功授权，并且重新请求排行榜
                        self.accredit = true;
                        this.getObtaintopSpot(this.shareBatchId,1)
                    },
                    fail: err => {
                    }
                });
                console.log("请求数据333");
            }
        },
    },
    //分享
    onShareAppMessage: function (res) {
        // console.log(res)
        // this.str = JSON.stringify(res)
        let batch_id =  wx.getStorageSync("batch_id"); 
        let that = this;
        
         wx.showShareMenu({
            withShareTicket: true
        });
        return {
            title: '分享测试',
            path: '/pages/topSpot/main?batch_id='+batch_id
        }
    },
}
</script>

<style lang="less" scoped>
</style>
